package cn.dyp.com.agriculture.dialog;

import android.app.DialogFragment;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import cn.dyp.com.agriculture.ClientApp;
import cn.dyp.com.agriculture.R;
import cn.dyp.com.agriculture.request.BaseRequest;
import cn.dyp.com.agriculture.request.BaseRequest.RequestResult;
import cn.dyp.com.agriculture.request.BaseRequest.OnResponseEventListener;
import cn.dyp.com.agriculture.request.RegisterRequest;
import cn.dyp.com.agriculture.request.RequestThread;

/**
 * 用户注册类
 * Created by lenovo on 2017/5/7.
 */
//DialogFragment是一种特殊的Fragment，用于在Activity的内容之上展示一个模态的对话框
public class RegisterDialog extends DialogFragment{
    private EditText mUsernameET,mPasswordET;
    private Button mCloseBtn,mPostBtn;
    private Context mContext;
    private ClientApp mApp;
    private RegisterRequest mRequest;

    public RegisterDialog(Context mContext){
        this.mContext = mContext; //保存来自构造函数中的参数
        this.mApp = (ClientApp) getActivity().getApplication();//getActivity():获得Fragment依附的Activity对象；getApplication():获得Application的对象
        mApp.setActivity(getActivity());
    }

    //碎片加载布局时调用
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));//把当前窗口背景设置为透明的
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉对话框标题
        return inflater.inflate(R.layout.register_dialog, container);
    }

    //与碎片相关联的活动一定已经创建完毕时调用
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRequest();
        init();
        setListener();
    }

    //设置请求对象，判断请求是否成功
    private void setRequest() {
        mRequest = new RegisterRequest("");
        mRequest.setOnResponseEventListener(new OnResponseEventListener(){//
            @Override
            public void onResponse(BaseRequest request, RequestResult result) {
                if (mRequest.isSuccess()) { //isSuccess是AgricultureRequest中的方法
                    showAlertDialog(mContext.getString(R.string.prompt), mContext.getString(R.string.register_success));
                    RegisterDialog.this.dismiss();
                } else {
                    showAlertDialog(mContext.getString(R.string.prompt), mContext.getString(R.string.register_failed));
                }
            }
        });
    }

    //控件实例化
    private void init() {
        mCloseBtn = (Button) getView().findViewById(R.id.close);
        mUsernameET = (EditText) getView().findViewById(R.id.username_et);
        mPasswordET = (EditText) getView().findViewById(R.id.password_et);
        mPostBtn = (Button) getView().findViewById(R.id.ok_button);
    }

    //用匿名类的方式注册监听器
    private void setListener() {
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterDialog.this.dismiss();
            }
        });
        mPostBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userRegister();
            }
        });
    }

    //用户注册
    private void userRegister() {
        String userName = mUsernameET.getText().toString().trim();
        String password = mPasswordET.getText().toString().trim();
        if (userName.equals("")) {
            showAlertDialog(mContext.getString(R.string.prompt), mContext.getString(R.string.username_not_empty));//提示，用户名不能为空
            return;
        }
        if (password.equals("")) {
            showAlertDialog(mContext.getString(R.string.prompt), mContext.getString(R.string.password_not_empty));//提示，密码不能为空
            return;
        }
        mRequest.setUserName(userName);
        mRequest.setPassword(password);
        startRequest(mRequest);
    }

    protected void showAlertDialog(String title, String message) {
        AlertDialog.Builder dialog= new AlertDialog.Builder(mContext);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton(mContext.getString(android.R.string.ok), null);//设置确认按钮的文字，但是不添加监听
        dialog.show();
    }

    /**
     * 启动网络连接线程
     * request 请求基类
     */
    protected void startRequest(BaseRequest request) {   //传入RegisterRequest
        if (request != null) {
            RequestThread thread = new RequestThread(mContext, mApp.getHandler());//传入上下文和消息处理对象
            thread.setRequest(request);
            thread.start();
        }
    }

}
